package com.ljxt.dt.web.servlet.basicinformationservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.SupplierType;
import com.ljxt.dt.service.basicinformationservice.SupplierListService;
import com.ljxt.dt.service.basicinformationservice.impl.SupplierListServiceImpl;
import com.ljxt.dt.util.DaoUtil;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.basicInformation.SupplierListVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-07 10:08
 * @copyright 老九学堂
 */
@WebServlet("/supplierList.html")
public class SupplierListServlet extends HttpServlet {

    private SupplierListService supplierListService = new SupplierListServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //查询供应商的类型
        List<SupplierType> supplierTypeList = supplierListService.selectSupplierType();

        //将集合保存在请求中
        req.setAttribute("typeList", supplierTypeList);
        req.getRequestDispatcher(req.getContextPath() + "WEB-INF/views/basicinformation/supplierList.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("进入post");
        //获取输入框参数
        String supplierName = req.getParameter("supplierName");
        String typeIdString = req.getParameter("typeId");
        String idString = req.getParameter("supplierId");
        //获取分页参数
        String currentIndexString = req.getParameter("currentIndex");
        String pageSizeString = req.getParameter("pageSize");


        //1.判断分页参数的初始值是否为空，若为空，赋默认值
        if(currentIndexString == null){
            currentIndexString = "1";
        }
        if(pageSizeString == null){
            pageSizeString = "5";
        }

        //将String类型的分页参数转换为int类型
        int currentIndex = Integer.parseInt(currentIndexString);

        int pageSize = Integer.parseInt(pageSizeString);
        //定义变量保存验证正整数的正则表达式规则
        String reg = "^[1-9]+$";
        //判断供应商id初始值，若为空串，或不是正整数，则给id赋值为-1
        if(idString.trim().length() == 0 || ! idString.matches(reg)){
            idString = "-1";
        }
        //将idString转换为int类型
        int id = Integer.parseInt(idString);
        //将typeIdString转换为int类型
        int typeId = Integer.parseInt(typeIdString);

        //执行分页查询方法查询数据
        List<SupplierListVO> supplierListVOS = supplierListService.findAll(currentIndex, pageSize, supplierName, typeId, id);
        //执行分页查询方法查询总数
        int total = supplierListService.selectCount(currentIndex, pageSize, supplierName, typeId, id);
        //定义一个变量保存查询
        PageVO supplierListVOPageVO = new PageVO(supplierListVOS, currentIndex, pageSize, total);

        JsonResult<PageVO<SupplierListVO>> jsonResult = new JsonResult<>();
        jsonResult.setData(supplierListVOPageVO);

        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(), jsonResult);

    }


}
